package com.lxw.config;

import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;

@Configuration
public class Config {

    private String exchangeName = "ex_routing_cms_postpage";

    @Value("${fhs.mq.queue}")
    private String queueName;

    @Value("${fhs.mq.routingKey}")
    private String routingKey;

    // 声明路由模式交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(exchangeName);
    }
    @Bean
    public Queue postPageMq() {
        return new Queue(queueName);//队列1
    }
    //
    @Bean
    public Binding dirBinding(DirectExchange directExchange, Queue postPageMq) {
        return BindingBuilder.bind(postPageMq).to(directExchange).with(routingKey);
    }


    @Bean
    public GridFSBucket gridFSBucket(MongoDbFactory mongoDbFactory){
        MongoDatabase db = mongoDbFactory.getDb();

        return GridFSBuckets.create(db);
    }
}
